"""
功能：通过刚度矩阵反演定位损伤
技术：动力学刚度修正法
比喻：通过心跳异常推断骨折位置
"""
import numpy as np
import numpy.linalg as la


def locate_damage(healthy_modes, damaged_modes):
    """确保矩阵维度正确 (n_nodes, n_modes)"""
    # 计算模态残差矩阵 (n_nodes, n_modes)
    residual = healthy_modes - damaged_modes

    # 计算伪逆矩阵 (n_modes, n_nodes)
    pinv_healthy = la.pinv(healthy_modes.T)  # 转置确保维度匹配

    # 灵敏度矩阵 (n_modes, n_nodes) @ (n_nodes, n_modes) → (n_modes, n_modes)
    S = pinv_healthy @ residual.T  # 转置残差矩阵

    # 按节点求和 (n_nodes,)
    damage_prob = np.sum(S ** 2, axis=0)

    # 归一化
    return damage_prob / np.max(damage_prob)